import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MyKeyFrame extends JFrame implements KeyListener {
    public MyKeyFrame() {
        initJFrame();
    }

    private void initJFrame() {
        this.setSize(500, 500);
        this.setLocationRelativeTo(null);
        this.setLayout(null);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.setVisible(true);

        JButton btn = new JButton("点我");
        btn.setBounds(0, 0, 100, 100);
//        匿名内部类实现事件监听
        btn.addKeyListener(this);
        this.getContentPane().add(btn);
    }

    @Override
    public void keyTyped(KeyEvent e) {
        System.out.println("键盘按下");
    }

    @Override
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        System.out.println("按下键盘不松开" + code);
    }

    @Override
    public void keyReleased(KeyEvent e) {
        System.out.println("松开键盘");
    }
}
